package com.aaa.day113.demo2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Description:
 * @author:LiWei
 * @date:2022年11月03日9:43
 */
public class Test3 {

    public static void main(String[] args) {

        List<Student> list=new ArrayList<>();
        list.add(new Student("张三",20));
        list.add(new Student("李四",21));
        list.add(new Student("王五",22));

//        Comparator<Student> comparator=new Comparator<Student>() {
//            //返回0表示o1和o2 排序时相同，若大于0 o1排序时大于
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getAge()-o2.getAge();
//            }
//        };

        Comparator<Student> comparator=(o1,o2)->{
          return o1.getAge()-o2.getAge();
        };
        Collections.sort(list,comparator);
        System.out.println(list);
    }
}



class Student{
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}